package algorithm;

import java.util.Stack;

public class Leetcode150 {

    public static int evalRPN(String[] tokens) {
        int sum = 0;
        int right;
        Stack<Integer> helper = new Stack<>();
        for (String tmp : tokens) {
            if (tmp.equals("+") || tmp.equals("-") || tmp.equals("*") || tmp.equals("/")) {
                right = helper.pop();
                if (!helper.isEmpty()) {
                    sum = helper.pop();
                }
                switch (tmp) {
                    case "+":
                        sum +=  right;
                        break;
                    case "-":
                        sum -= right;
                        break;
                    case "*":
                        sum *= right;
                        break;
                    case "/":
                        sum /= right;
                        break;
                }
                helper.push(sum);

            } else {
                helper.push(Integer.parseInt(tmp));
            }
        }

        if (!helper.isEmpty()) {
            sum = helper.pop();
        }
        return sum;

    }

    public static void main(String[] args) {
        String[] tokens = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        evalRPN(tokens);
    }
}
